Mã giả Dạng hàng bậc thang

Mã giả sau đây chuyển đổi một ma trận về dạng hàng bậc thang rút gọn:

function ToReducedRowEchelonForm(Matrix M) is    lead:= 0    rowCount:= the number of rows in M    columnCount:= the number of columns in M    for 0 ≤ r < rowCount do        if columnCount ≤ lead then            stop function        end if        i = r        while M[i, lead] = 0 do            i = i + 1            if rowCount = i then                i = r                lead = lead + 1                if columnCount = lead then                    stop function                end if            end if        end while        if i ≠ r then Swap rows i and r        Divide row r by M[r, lead]        for 0 ≤ i < rowCount do            if i ≠ r do                Subtract M[i, lead] multiplied by row r from row i            end if        end for        lead = lead + 1    end forend function

Mã giả sau đây chuyển ma trận về dạng hàng bậc thang (chưa rút gọn):

function ToRowEchelonForm(Matrix M) is    nr:= number of rows in M    nc:= number of columns in M        for 0 ≤ r < nr do        allZeros:= true        for 0 ≤ c < nc do            if M[r, c] != 0 then                allZeros:= false                exit for            end if        end for        if allZeros = true then            In M, swap row r with row nr            nr:= nr - 1        end if    end for        p:= 0    while p < nr and p < nc do        label nextPivot:            r:= 1            while M[p, p] = 0 do                 if (p + r) <= nr then                    p:= p + 1                    goto nextPivot                end if                In M, swap row p with row (p + r)                r:= r + 1            end while            for 1 ≤ r < (nr - p) do                 if M[p + r, p] != 0 then                    x:= -M[p + r, p] / M[p, p]                    for p ≤ c < nc do                        M[p + r, c]:= M[p, c] * x + M[p + r, c]                    end for                end if            end for            p:= p + 1    end whileend function

Liên quan